﻿package com.wandian.util
{
	import flash.utils.getTimer;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class StopWatch extends Object
	{
		private var _started:Boolean=false;
		private var _startTimeKeys:Array;
		private var _stopTimeKeys:Array;
		private var _title:String;

		public function StopWatch(sin:Single)
		{
			if (sin == null)
				throw new Error("Can\'t create instance , Single is Null!");
			this.reset();
			return;
		} 

		public function start(title:String=""):void
		{
			if (!this._started)
			{
				this._title=title;
				this._started=true;
				this._startTimeKeys.push(getTimer());
			}
			return;
		} 

		public function stop():void
		{
			var stopTime:int=0;
			if (this._started)
			{
				stopTime=getTimer();
				this._stopTimeKeys[(this._startTimeKeys.length - 1)]=stopTime;
				this._started=false;
			}
			return;
		} 

		public function reset():void
		{
			this._startTimeKeys=[];
			this._stopTimeKeys=[];
			this._started=false;
			return;
		} 

		public function getCurrentNow():String
		{
			return this.format(getTimer());
		}

		public function toString():String
		{
			var startTime:int=0;
			var stopTime:int=0;
			var message:String="\n ********************* [STOPWATCH] *********************";
			if (this._title != "")
			{
				message=message + ("\n * " + this._title);
			}
			var index:int=0;
			while (index < this._startTimeKeys.length)
			{

				startTime=this._startTimeKeys[index];
				stopTime=this._stopTimeKeys[index];
				message=message + ("\n * started [" + this.format(startTime) + "ms] stopped [" + this.format(stopTime) + "ms] time [" + this.format(stopTime - startTime) + "ms]");
				index=index + 1;
			}
			if (index == 0)
			{
				message=message + "\n * never started.";
			}
			else
			{
				message=message + ("\n * total runnning time: " + this.timeInSeconds + "s");
			}
			message=message + "\n *******************************************************";
			return message;
		} 

		public function get started():Boolean
		{
			return this._started;
		} 

		public function get timeInMilliSeconds():int
		{
			if (this._started)
			{
				this._stopTimeKeys[(this._startTimeKeys.length - 1)]=getTimer();
			}
			var totalTime:int=0;
			var index:int=0;
			while (index < this._startTimeKeys.length)
			{

				totalTime=totalTime + (this._stopTimeKeys[index] - this._startTimeKeys[index]);
				index=index + 1;
			}
			return totalTime;
		} 

		public function get timeInSeconds():Number
		{
			return this.timeInMilliSeconds / 1000;
		} 

		private function format(source:int):String
		{
			var msg:String="";
			var length:int=source.toString().length;
			var count:int=0;
			while (count < 5 - length)
			{

				msg=msg + "0";
				count=count + 1;
			}
			return msg + source;
		} 
		private static var _instance:StopWatch=null;

		public static function getInstance():StopWatch
		{
			if (_instance == null)
			{
				_instance=new StopWatch(new Single());
			}
			return _instance;
		}
		
		static public function getDateString(date:Date):String
		{
			var dYear:String = String(date.getFullYear());
			
			var dMouth:String = String((date.getMonth() + 1 < 10) ? "0" : "") + (date.getMonth() + 1);
			
			var dDate:String = (date.getDate() < 10) ? "0" : "";
			dDate+=+ date.getDate();
			var ret:String = "";
			
			ret += dYear + "/" + dMouth + "/" + dDate + " ";
			
			ret += DAYS[date.getDay()] + "";
			
			ret += ((date.getHours() < 10) ? "0" : "") + date.getHours();
			
			ret += ((date.getMinutes() < 10) ? "0" : "") + date.getMinutes();
			
			// 想要获取秒的话，date.getSeconds() ，语句同小时、分
			
			return ret;
		}
		static public const DAYS:Array = ["周日","周一","周二","周三","周四","周五","周六"];

	}
}

class Single
{

}
